package com.hang.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hang.common.BizErrorCodeEnum;
import com.hang.common.BizException;
import com.hang.mapper.ClazzMapper;
import com.hang.mapper.ClazzStudentMapper;
import com.hang.mapper.TeacherMapper;
import com.hang.model.Clazz;
import com.hang.model.ClazzStudent;
import com.hang.model.Student;
import com.hang.model.Teacher;
import com.hang.service.StudentService;
import com.hang.mapper.StudentMapper;
import com.hang.util.JwtUtil;
import com.hang.util.listener.StudentListener;
import com.hang.vo.LoginVo;
import com.hang.vo.StudentVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService{

    @Autowired
    private ClazzMapper clazzMapper;

    @Autowired
    private ClazzStudentMapper clazzStudentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    /**
     * 导入学生信息
     * @param multipartFile
     */
    @Override
    public void insertInformation(MultipartFile multipartFile) {
        try{
            EasyExcel.read(multipartFile.getInputStream(), StudentVo.class,new StudentListener(baseMapper))
                    .sheet()
                    .doRead();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    @Override
    public Map<String, Object> login(LoginVo loginVo) {
        if(ObjectUtils.isEmpty(loginVo)){
            throw new BizException(BizErrorCodeEnum.OBJECT_IS_NULL);
        }

        if(StringUtils.isBlank(loginVo.getLoginId())||StringUtils.isBlank(loginVo.getPassword())){
            throw new BizException(BizErrorCodeEnum.PARAM_IS_NULL);
        }

        //先去老师的数据库中进行用户密码校验
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id",loginVo.getLoginId());
        queryWrapper.eq("password",loginVo.getPassword());

        Student student = baseMapper.selectOne(queryWrapper);
        if(ObjectUtils.isEmpty(student)){
            throw new BizException(BizErrorCodeEnum.LOGIN_FAIL);
        }

        //校验登录成功
        Map<String,Object> map = new HashMap<>();
        map.put("studentId",student.getStudentId());
        map.put("realName",student.getRealName());

//        String token = JwtHelper.createStudentToken(student.getStudentId());

        String token = JwtUtil.createStudentToken(student.getStudentId());

        map.put("token",token);

        return map;
    }

    @Override
    public List<Clazz> getHisList(String studentId, Integer page, Integer size) {
        //首先需要找出这个学生对应的所有班级号
        QueryWrapper<ClazzStudent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id",studentId);

        Page<ClazzStudent> pages = new Page<>(page,size);
        IPage<ClazzStudent> iPage = clazzStudentMapper.selectPage(pages,queryWrapper);

        List<ClazzStudent> clazzStudentList = iPage.getRecords();

        //避免大量扩容
        List<Clazz> result = new ArrayList<>(clazzStudentList.size());

        for (ClazzStudent clazzStudent : clazzStudentList){
            if(!ObjectUtils.isEmpty(clazzStudent)){
                QueryWrapper<Clazz> clazzQueryWrapper = new QueryWrapper<>();
                clazzQueryWrapper.eq("clazz_id",clazzStudent.getClazzId());

                Clazz clazz = clazzMapper.selectOne(clazzQueryWrapper);

                if(!ObjectUtils.isEmpty(clazz)){
                    //查找出班级任课老师的姓名(V1.0.1)
                    QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
                    teacherQueryWrapper.eq("teacher_id",clazz.getTeacherId());
                    clazz.setTeacherName(teacherMapper.selectOne(teacherQueryWrapper).getRealName());
                    result.add(clazz);
                }
            }
        }

        //返回结果集
        return result;
    }

    @Override
    public boolean hasMoreData(String studentId, Integer page, Integer size) {
        //首先需要找出这个学生对应的所有班级号
        QueryWrapper<ClazzStudent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id",studentId);

        Integer totals = clazzStudentMapper.selectCount(queryWrapper);

        return totals>page*size ? Boolean.TRUE:Boolean.FALSE;
    }
}